package com.ruoyi.stuclient.service.impl;


import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.ruoyi.common.core.context.ThirdSessionHolder;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.stuclient.domain.EwcChapter;
import com.ruoyi.stuclient.domain.EwcCourseGrouping;
import com.ruoyi.stuclient.domain.dto.StudentLoginDto;
import com.ruoyi.stuclient.domain.vo.*;
import com.ruoyi.stuclient.mapper.EwcCourseGroupingMapper;
import com.ruoyi.stuclient.mapper.EwcStudentGroupMapper;
import com.ruoyi.stuclient.service.IEwcStudentGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 学生信息Service业务层处理
 * 
 * @author zsp
 * @date 2025-03-18
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class EwcStudentGroupServiceImpl extends MPJBaseServiceImpl<EwcCourseGroupingMapper, EwcCourseGrouping> implements IEwcStudentGroupService {

    @Autowired
    private EwcStudentGroupMapper ewcStudentGroupMapper;

    @Override
    public StudentGroupVo getStudentGroupInfo(StudentLoginDto studentLoginDto) {
        return ewcStudentGroupMapper.getStudentGroupInfo(studentLoginDto);
    }

    @Override
    public boolean updateBySIDAndGID(EwcCourseGrouping ewcCourseGrouping) {
        QueryWrapper<EwcCourseGrouping> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("grouping_id", ewcCourseGrouping.getGroupingId());
        queryWrapper.eq("student_id", ewcCourseGrouping.getStudentId());
        EwcCourseGrouping courseGrouping = this.getOne(queryWrapper);
        Integer submitCount = 1;
        if(!Objects.isNull(courseGrouping)){
            courseGrouping.setSubmitCount(courseGrouping.getSubmitCount()+1);
            submitCount = courseGrouping.getSubmitCount();
        }
        Date currentDate = new Date();
        // 创建 UpdateWrapper 对象
        UpdateWrapper<EwcCourseGrouping> updateWrapper = new UpdateWrapper<>();
        // 设置更新条件
        updateWrapper.eq("grouping_id", ewcCourseGrouping.getGroupingId())
                .eq("student_id", ewcCourseGrouping.getStudentId());
        EwcCourseGrouping entity = new EwcCourseGrouping();
        entity.setUpdateTime(currentDate);
        entity.setVideoPath(ewcCourseGrouping.getVideoPath());
        entity.setTaskId(ewcCourseGrouping.getTaskId());
        entity.setSubmitTime(currentDate);
        entity.setSubmitCount(submitCount);
        entity.setScore(BigDecimal.valueOf(0));
        entity.setTotalScore(BigDecimal.valueOf(0));
        entity.setSynthesisGesture(BigDecimal.valueOf(0));
        entity.setSynthesisRhythm(BigDecimal.valueOf(0));
        entity.setSynthesisSpeed(BigDecimal.valueOf(0));
        entity.setScoreDescription("");
        entity.setAimodelDescriotion("");
        entity.setAnalysisVideoPath("");
        return this.update(entity,updateWrapper);
    }

    @Override
    public StudentGroupSubmitVo getStudentGroupSubmitInfo(String groupingId) {
        StudentGroupSubmitVo studentGroupSubmitVo = ewcStudentGroupMapper.getStudentGroupSubmitInfo(groupingId);
        List<GroupSubmitListVo> groupSubmitListVo = ewcStudentGroupMapper.getGroupSubmitInfoList(groupingId);
        studentGroupSubmitVo.setGroupSubmitListVo( groupSubmitListVo);
        return studentGroupSubmitVo;
    }

    @Override
    public GroupSubmitAnalysisVo getGroupSubmitAnalysisInfo(String groupingId, String studentId) {
        return ewcStudentGroupMapper.getGroupSubmitAnalysisInfo(groupingId,studentId);
    }

    @Override
    public List<StudentGroupCountVo> getStudentGroupList(String chapterId, String courseId, String classId) {
        return ewcStudentGroupMapper.getStudentGroupList(ThirdSessionHolder.getThirdSession().getId(),chapterId,courseId,classId);
    }
}
